//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package database;

import entity.Column;
import entity.Table;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import xml.XmlUtil;

public class Util {
    private String dbType;
    private String driverName;
    private String userName;
    private String passWord;
    private String url;
    private String dbName;
    private String ip;
    
    public Util() {
    }
    
    public String getIp() {
        return this.ip;
    }
    
    public void setIp(String ip) {
        this.ip = ip;
    }
    
    public String getDbType() {
        return this.dbType;
    }
    
    public void setDbType(String dbType) {
        this.dbType = dbType;
    }
    
    public String getDbName() {
        return this.dbName;
    }
    
    public void setDbName(String dbName) {
        this.dbName = dbName;
    }
    
    public String getDriverName() {
        return this.driverName;
    }
    
    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }
    
    public String getUserName() {
        return this.userName;
    }
    
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String getPassWord() {
        return this.passWord;
    }
    
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
    
    public String getUrl() {
        return this.url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    
    public List<String> getSchemas() throws ClassNotFoundException, SQLException {
        Class.forName(this.driverName);
        Connection connection = DriverManager.getConnection(this.url, this.userName, this.passWord);
        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet rs = metaData.getCatalogs();
        ArrayList list = new ArrayList();
        
        while(rs.next()) {
            list.add(rs.getString(1));
        }
        
        rs.close();
        connection.close();
        return list;
    }
    
    public List<Table> getDbInfo() throws ClassNotFoundException, SQLException {
        Map<String, String> convertMap = XmlUtil.readNu("typeConverter.xml");
        Class.forName(this.driverName);
        Properties props = new Properties();
        props.put("remarksReporting", "true");
        props.put("user", this.userName);
        props.put("password", this.passWord);
        if (this.dbName != null) {
            this.url = this.url.replace("[db]", this.dbName);
        }
        
        if (this.ip != null && !this.ip.equals("")) {
            this.url = this.url.replace("[ip]", this.ip);
        } else {
            this.url = this.url.replace("[ip]", "127.0.0.1");
        }
        
        Connection connection = DriverManager.getConnection(this.url, props);
        DatabaseMetaData metaData = connection.getMetaData();
        String schema = null;
        String catalog = null;
        if (this.dbType != null && this.dbType.toUpperCase().indexOf("ORACLE") >= 0) {
            schema = this.userName.toUpperCase();
            catalog = connection.getCatalog();
        }
        
        ResultSet tablers = metaData.getTables(catalog, schema, (String)null, new String[]{"TABLE"});
        ArrayList list = new ArrayList();
        
        while(true) {
            Table table;
            String tableName;
            do {
                do {
                    if (!tablers.next()) {
                        tablers.close();
                        connection.close();
                        return list;
                    }
                    
                    table = new Table();
                    tableName = tablers.getString("TABLE_NAME");
                } while(tableName.indexOf("=") >= 0);
            } while(tableName.indexOf("$") >= 0);
            
            if (tableName.toUpperCase().equals(tableName)) {
                table.setName(tableName.toLowerCase());
            } else {
                table.setName(tableName);
            }
            
            table.setComment(tablers.getString("REMARKS"));
            ResultSet primaryKeys = metaData.getPrimaryKeys(catalog, schema, tableName);
            
            ArrayList keys;
            String keyname;
            for(keys = new ArrayList(); primaryKeys.next(); keys.add(keyname)) {
                keyname = primaryKeys.getString("COLUMN_NAME");
                if (keyname.toUpperCase().equals(keyname)) {
                    keyname = keyname.toLowerCase();
                }
            }
            
            System.out.println("信息：" + catalog + "   " + schema + "   " + tableName);
            ResultSet columnrs = metaData.getColumns(catalog, schema, tableName, (String)null);
            ArrayList columnList = new ArrayList();
            
            while(columnrs.next()) {
                Column column = new Column();
                String columnName = columnrs.getString("COLUMN_NAME");
                if (columnName.toUpperCase().equals(columnName)) {
                    columnName = columnName.toLowerCase();
                }
                
                column.setColumnName(columnName);
                String columnDbType = columnrs.getString("TYPE_NAME");
                column.setColumnDbType(columnDbType);
                String typeName = (String)convertMap.get(columnDbType);
                if (typeName == null) {
                    typeName = columnrs.getString("TYPE_NAME");
                }
                
                column.setColumnType(typeName);
                String remarks = columnrs.getString("REMARKS");
                if (remarks == null) {
                    remarks = columnName;
                }
                
                column.setColumnComment(remarks);
                if (keys.contains(columnName)) {
                    column.setColumnKey("PRI");
                    table.setKey(column.getColumnName());
                } else {
                    column.setColumnKey("");
                }
                
                int decimal_digits = columnrs.getInt("DECIMAL_DIGITS");
                if (decimal_digits > 0) {
                    column.setColumnType("Double");
                }
                
                column.setDecimal_digits(decimal_digits);
                column.setColums_size(columnrs.getInt("COLUMN_SIZE"));
                columnList.add(column);
            }
            
            columnrs.close();
            table.setColumns(columnList);
            list.add(table);
        }
    }
}
